/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package com.rest.business;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
import com.rest.client.RestClient;
import com.rest.constant.Constant;
import com.rest.exception.BusinessException;
import com.rest.response.AreaResponse;
import com.rest.response.AxisResponse;
import com.rest.response.BaseBooleanResponse;
import com.rest.response.BaseJsonResponse;
import com.rest.response.BaseResponse;
import com.rest.response.CheckResponse;
import com.rest.response.CollectClassResponse;
import com.rest.response.CollectStudentResponse;
import com.rest.response.CompareFaceResponse;
import com.rest.response.DeviceResponse;
import com.rest.response.FaceResponse;
import com.rest.response.GradeClassResponse;
import com.rest.response.IaBaseResponse;
import com.rest.response.IsTaskDataResponse;
import com.rest.response.LoginResponse;
import com.rest.response.QRCodeResponse;
import com.rest.response.QualifiedResponse;
import com.rest.response.SchoolNameResponse;
import com.rest.response.SchoolResponse;
import com.rest.response.StudentClassResponse;
import com.rest.response.StudentInfoResponse;
import com.rest.response.StudentOtherResponse;
import com.rest.response.StudentResponse;
import com.rest.response.StudentSEQ;
import com.rest.response.TaskListResponse;
import com.rest.response.TaskWorkerResponse;
import com.rest.response.UserInfoResponse;
import com.rest.response.ValidResponse;
import com.rest.response.YearResponse;
import com.rest.util.JsonUtil;
import com.rest.util.MD5Coder;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import jz.joyoung.robot.util.LogUtil;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class RestProxy {
    private static final String TAG = "jd-" + RestProxy.class.getSimpleName();
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private static Gson gson;

    private IRestAPI mRestApi;

    private String mPhoneNum, mPassword;

    private static class SingletonHolder {
        private static final RestProxy INSTANCE = new RestProxy();
    }

    /**
     * 获取单例
     */
    public static RestProxy getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private RestProxy() {
        OkHttpClient okHttpClient = RestClient.getInstance().getOkHttpClient();

        Retrofit retrofitRms = new Retrofit.Builder()
                .client(okHttpClient)//设置网络访问框架
                .baseUrl(Constant.HOST)
                .addConverterFactory(GsonConverterFactory.create(buildGson()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//让Retrofit支持RxJava
                .build();


        mRestApi = retrofitRms.create(IRestAPI.class);
    }

    /**
     * 增加后台返回""和"null"的处理
     * 1.int=>0
     * 2.double=>0.00
     * 3.long=>0L
     *
     */
    private Gson buildGson() {
        if (gson == null) {
            gson = new GsonBuilder()
                    .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
                    .registerTypeAdapter(int.class, new IntegerDefault0Adapter())
                    .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                    .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                    .registerTypeAdapter(Long.class, new LongDefault0Adapter())
                    .registerTypeAdapter(long.class, new LongDefault0Adapter())
                    .create();
        }
        return gson;
    }

    public class IntegerDefault0Adapter implements JsonSerializer<Integer>, JsonDeserializer<Integer> {

        @Override
        public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                if (json.getAsString().equals("") || json.getAsString().equals("null")) {
                    //定义为int类型,如果后台返回""或者null,则返回0
                    return 0;
                }
            } catch (Exception ignore) {
            }
            try {
                return json.getAsInt();
            } catch (NumberFormatException e) {
                throw new JsonSyntaxException(e);
            }
        }

        @Override
        public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src);
        }
    }

    public class LongDefault0Adapter implements JsonSerializer<Long>, JsonDeserializer<Long> {

        @Override
        public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                if (json.getAsString().equals("") || json.getAsString().equals("null")) {
                    //定义为long类型,如果后台返回""或者null,则返回0
                    return Long.valueOf(0);
                }
            } catch (Exception ignore) {
            }
            try {
                return json.getAsLong();
            } catch (NumberFormatException e) {
                throw new JsonSyntaxException(e);
            }
        }

        @Override
        public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src);
        }
    }

    public class DoubleDefault0Adapter implements JsonSerializer<Double>, JsonDeserializer<Double> {

        @Override
        public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                if (json.getAsString().equals("") || json.getAsString().equals("null")) {
                    //定义为double类型,如果后台返回""或者null,则返回0.00
                    return 0.00;
                }
            } catch (Exception ignore) {
            }
            try {
                return json.getAsDouble();
            } catch (NumberFormatException e) {
                throw new JsonSyntaxException(e);
            }
        }

        @Override
        public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src);
        }
    }

    /**
     * 显式时间 00:00
     *
     * @param hour 小时数
     * @param min  分钟数
     * @return 时间 00:00
     */
    public String formatTime(int hour, int min) {
        return String.format(Locale.getDefault(), "%02d:%02d", hour, min);
    }

    /**
     * 生成签名
     */
    private String getSign(String str, byte[] data, String salt) {
        if (TextUtils.isEmpty(str)) {
            LogUtil.w(TAG, "Empty param!");
            return null;
        }

        byte[] strBytes;
        byte[] saltBytes;
        try {
            saltBytes = salt.getBytes("UTF-8");
            strBytes = str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException | NullPointerException e) {
            e.printStackTrace();
            return null;
        }

        int strLength = strBytes.length;
        int dataLength = data == null ? 0 : data.length;
        int saltLength = saltBytes.length;
        byte[] valuesBytes = new byte[strLength + dataLength + saltLength];
        System.arraycopy(strBytes, 0, valuesBytes, 0, strLength);
        if (dataLength > 0) {
            System.arraycopy(data, 0, valuesBytes, strLength, dataLength);
        }
        System.arraycopy(saltBytes, 0, valuesBytes, strLength + dataLength, saltLength);
        MD5Coder md5Coder = new MD5Coder();
        return md5Coder.encrypt2Str(valuesBytes);
    }


    public String md5Decode(String content) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(content.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("NoSuchAlgorithmException", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UnsupportedEncodingException", e);
        }
        //对生成的16字节数组进行补零操作
        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }

    public void getAreaByLevel(String parentId, String token, Observer<AreaResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("parentId", parentId)
                .add("token", token)
                .build();

        mRestApi.getAreaByLevel(Constant.HOST + Constant.BASE_URL + Constant.METHOD_GET_AREA_LEVEL, requestBody)
                .map(new Function<AreaResponse, AreaResponse>() {
                    @Override
                    public AreaResponse apply(AreaResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void login(String account, String password, Observer<LoginResponse> subscriber) {

        Map<String, Object> map = new HashMap<>();//body  map
        map.put("account", account);
        map.put("password", password);
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);
        mRestApi.login(Constant.USER_HOST + Constant.METHOD_LOGIN, requestBody)
                .map(new Function<LoginResponse, LoginResponse>() {
                    @Override
                    public LoginResponse apply(LoginResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getSchool(String areaId, String parentId, String token, Observer<SchoolResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("parentId", parentId)
                .add("areaId", areaId)
                .add("token", token)
                .build();

        mRestApi.getSchool(Constant.HOST + Constant.BASE_URL + Constant.METHOD_GET_SCHOOL, requestBody)
                .map(new Function<SchoolResponse, SchoolResponse>() {
                    @Override
                    public SchoolResponse apply(SchoolResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getStudentList(String taskId, String classId, String status, String studentName, Observer<StudentResponse> subscriber) {


        mRestApi.getStudentList(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_STUDENT_LIST, taskId, classId, status, studentName, 1000)
                .map(new Function<StudentResponse, StudentResponse>() {
                    @Override
                    public StudentResponse apply(StudentResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void notExamReason(String taskId, String studentId, String notExamReason, Observer<IaBaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder()
                .add("taskId", taskId)
                .add("studentId", studentId)
                .add("notExamReason", notExamReason)
                .build();


        mRestApi.notExamReason(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_STUDENT_REASON, requestBody)
                .map(new Function<IaBaseResponse, IaBaseResponse>() {
                    @Override
                    public IaBaseResponse apply(IaBaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void isShowOther(String taskId, Observer<AxisResponse> subscriber) {


        mRestApi.isShowOther(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_STUDENT_ISSHOWOTHER, taskId)
                .map(new Function<AxisResponse, AxisResponse>() {
                    @Override
                    public AxisResponse apply(AxisResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getStudentInfoSL(String studentId, String taskId, Observer<StudentInfoResponse> subscriber) {
        mRestApi.getStudentInfoSL(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_STUDENT_INFO_SL, studentId, taskId)
                .map(new Function<StudentInfoResponse, StudentInfoResponse>() {
                    @Override
                    public StudentInfoResponse apply(StudentInfoResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getStudentInfoQG(String studentId, String taskId, Observer<StudentInfoResponse> subscriber) {


        mRestApi.getStudentInfoQG(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_STUDENT_INFO_QG, studentId, taskId)
                .map(new Function<StudentInfoResponse, StudentInfoResponse>() {
                    @Override
                    public StudentInfoResponse apply(StudentInfoResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getOtherExamineOptions(String studentId, String taskId, Observer<StudentOtherResponse> subscriber) {

        Map<String, Object> map = new HashMap<>();//body  map
        map.put("taskId", taskId);
        map.put("studentId", studentId);

        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);


        mRestApi.getOtherExamineOptions(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_STUDENT_INFO_OTHER, requestBody)
                .map(new Function<StudentOtherResponse, StudentOtherResponse>() {
                    @Override
                    public StudentOtherResponse apply(StudentOtherResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void saveStudentFirstInfo(String studentId, String isFrameglass, String isContactlenses, String isOkglasses, String osVision, String odVision, String osEyesight, String odEyesight, String odOk, String osOk, String odFrameglass, String osFrameglass, String odContactlenses, String osContactlenses, String taskId, String remark, String source, String device, Observer<QualifiedResponse> subscriber) {

        if (isOkglasses.equals("0")) {
            odOk = "";
            osOk = "";
        }
        Map<String, Object> map = new HashMap<>();//body  map
        map.put("studentId", studentId);
        map.put("odEyesight", odEyesight);
        map.put("osEyesight", osEyesight);
        map.put("odVision", odVision);
        map.put("osVision", osVision);
        map.put("isOkglasses", isOkglasses);
        map.put("isContactlenses", isContactlenses);
        map.put("taskId", taskId);
        map.put("isFrameglass", isFrameglass);
        map.put("odOk", odOk);
        map.put("osOk", osOk);
        map.put("odFrameglass", odFrameglass);
        map.put("osFrameglass", osFrameglass);
        map.put("odContactlenses", odContactlenses);
        map.put("osContactlenses", osContactlenses);
        map.put("remark", remark);
        map.put("source", source);
        map.put("device", device);
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);


        mRestApi.saveStudentFirstInfo(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_SAVE_STUDENT_FIRST_INFO, requestBody)
                .map(new Function<QualifiedResponse, QualifiedResponse>() {
                    @Override
                    public QualifiedResponse apply(QualifiedResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void updateStudentFirstInfo(String id, String studentId, String isFrameglass, String isContactlenses, String isOkglasses, String osVision, String odVision, String osEyesight, String odEyesight, String odOk, String osOk, String odFrameglass, String osFrameglass, String odContactlenses, String osContactlenses, String taskId, String remark, String source, String device, Observer<QualifiedResponse> subscriber) {

        if (isOkglasses.equals("0")) {
            odOk = "";
            osOk = "";
        }

        Map<String, Object> map = new HashMap<>();//body  map
        map.put("id", id);
        map.put("studentId", studentId);
        map.put("odEyesight", odEyesight);
        map.put("osEyesight", osEyesight);
        map.put("odVision", odVision);
        map.put("osVision", osVision);
        map.put("isOkglasses", isOkglasses);
        map.put("isContactlenses", isContactlenses);
        map.put("odFrameglass", odFrameglass);
        map.put("osFrameglass", osFrameglass);
        map.put("odContactlenses", odContactlenses);
        map.put("osContactlenses", osContactlenses);
        map.put("taskId", taskId);
        map.put("isFrameglass", isFrameglass);
        map.put("odOk", odOk);
        map.put("osOk", osOk);
        map.put("remark", remark);
        map.put("source", source);
        map.put("device", device);
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);


        mRestApi.updateStudentFirstInfo(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_UPDATE_STUDENT_FIRST_INFO, requestBody)
                .map(new Function<QualifiedResponse, QualifiedResponse>() {
                    @Override
                    public QualifiedResponse apply(QualifiedResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void saveStudentSecondInfo(Map<String, Object> map, Observer<QualifiedResponse> subscriber) {
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);

        mRestApi.saveStudentSecondInfo(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_SAVE_STUDENT_SECOND_INFO, requestBody)
                .map(new Function<QualifiedResponse, QualifiedResponse>() {
                    @Override
                    public QualifiedResponse apply(QualifiedResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void addStudentInfoQt(Map<String, Object> map, Observer<QualifiedResponse> subscriber) {
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);

        mRestApi.addStudentInfoQt(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_SAVE_STUDENT_OTHER_SAVE, requestBody)
                .map(new Function<QualifiedResponse, QualifiedResponse>() {
                    @Override
                    public QualifiedResponse apply(QualifiedResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void editStudentInfoQt(Map<String, Object> map, Observer<QualifiedResponse> subscriber) {
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);

        mRestApi.editStudentInfoQt(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_SAVE_STUDENT_OTHER_UPDATE, requestBody)
                .map(new Function<QualifiedResponse, QualifiedResponse>() {
                    @Override
                    public QualifiedResponse apply(QualifiedResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void updateStudentSecondInfo(Map<String, Object> map, Observer<QualifiedResponse> subscriber) {


        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);


        mRestApi.updateStudentSecondInfo(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_UPDATE_STUDENT_SECOND_INFO, requestBody)
                .map(new Function<QualifiedResponse, QualifiedResponse>() {
                    @Override
                    public QualifiedResponse apply(QualifiedResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void saveStudent(String name, String hometown, String nation, String birth, String sex, String idcard, String passport, String orgId, String classId, String taskId, String stuType, String schoolRoll, String code, Observer<BaseResponse> subscriber) {


//        RequestBody requestBody = new FormBody.Builder()
//                .add("name", name)
//                .add("nativePlace", hometown)
//                .add("minority", nation)
//                .add("birth", birth)
//                .add("sex", sex)
//                .add("idcard", idcard)
//                .add("passport", passport)
//                .add("orgId",orgId)
//                .add("classId",classId)
//                .add("stuType",stuType)//1-大陆，2-非大陆
//                .build();

        Map<String, Object> map = new HashMap<>();//body  map
        map.put("name", name);
        map.put("nativePlace", hometown);
        map.put("minority", nation);
        map.put("birth", birth);
        map.put("sex", sex);
        map.put("idcard", idcard);
        map.put("passport", passport);
        map.put("orgId", orgId);
        map.put("classId", classId);
        map.put("stuType", stuType);
        map.put("taskId", taskId);
        map.put("schoolRoll", schoolRoll);
        map.put("code", code);

        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);


        mRestApi.saveStudent(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_SAVE_STUDENT, requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getSchoolName(String classId, String token, Observer<SchoolNameResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("classId", classId)
                .add("token", token)
                .build();

        mRestApi.getSchoolName(Constant.HOST + Constant.BASE_URL + Constant.METHOD_GET_SCHOOL_NAME, requestBody)
                .map(new Function<SchoolNameResponse, SchoolNameResponse>() {
                    @Override
                    public SchoolNameResponse apply(SchoolNameResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void saveCheck(String eyesightId, String diopterId, String diagnoseInit, String docAdvice, String reviewDepart, String other, String id, String token, Observer<BaseResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("hisCheckDiagnose.examId", eyesightId)
                .add("hisCheckDiagnose.examId", diopterId)
                .add("hisCheckDiagnose.diagnoseInit", diagnoseInit)
                .add("hisCheckDiagnose.docAdvice", docAdvice)
                .add("hisCheckDiagnose.reviewDepart", reviewDepart)
                .add("hisCheckDiagnose.other", other)
                .add("hisCheckDiagnose.id", id)
                .add("token", token)
                .build();

        mRestApi.saveCheck(Constant.HOST + Constant.BASE_URL + Constant.METHOD_SAVE_CHECK, requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getCheck(String eyesightId, String diopterId, String token, Observer<CheckResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("examId", eyesightId)
                .add("examId", diopterId)
                .add("token", token)
                .build();

        mRestApi.getCheck(Constant.HOST + Constant.BASE_URL + Constant.METHOD_GET_CHECK, requestBody)
                .map(new Function<CheckResponse, CheckResponse>() {
                    @Override
                    public CheckResponse apply(CheckResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getStudentById(String studentId, String token, Observer<StudentClassResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("id", studentId)
                .add("token", token)
                .build();

        mRestApi.getStudentById(Constant.HOST + Constant.BASE_URL + Constant.METHOD_GET_STUDENT_BY_ID, requestBody)
                .map(new Function<StudentClassResponse, StudentClassResponse>() {
                    @Override
                    public StudentClassResponse apply(StudentClassResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getSchoolIdByName(String areaName, String token, Observer<IaBaseResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("areaName", areaName)
                .add("token", token)
                .build();

        mRestApi.getSchoolIdByName(Constant.HOST + Constant.BASE_URL + Constant.METHOD_GET_SCHOOOL_ID_BY_NAME, requestBody)
                .map(new Function<IaBaseResponse, IaBaseResponse>() {
                    @Override
                    public IaBaseResponse apply(IaBaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getAllClasses(String taskId, int status, Observer<GradeClassResponse> subscriber) {


        mRestApi.getGradeClass(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_ALL_CLASS, taskId, status, 1000)
                .map(new Function<GradeClassResponse, GradeClassResponse>() {
                    @Override
                    public GradeClassResponse apply(GradeClassResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getTaskList(int currentPage,String taskStatus, String taskName, String taskId, String userId, String planYear, String planType, Observer<TaskListResponse> subscriber) {

//        RequestBody requestBody = new FormBody.Builder()
//                .add("taskStatus", taskStatus)
//                .add("token", token)
//                .build();

        mRestApi.getTaskList(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_LIST_TASK, taskStatus, taskName, currentPage, 50, taskId, userId, planYear, planType)
                .map(new Function<TaskListResponse, TaskListResponse>() {
                    @Override
                    public TaskListResponse apply(TaskListResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getGrade(String id, String token, Observer<SchoolResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("id", id + "")
                .add("token", token)
                .build();

        mRestApi.getGrade(Constant.HOST + Constant.BASE_URL + Constant.METHOD_GET_GRADE, requestBody)
                .map(new Function<SchoolResponse, SchoolResponse>() {
                    @Override
                    public SchoolResponse apply(SchoolResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getClass(String schoolId, String classYear, String token, Observer<SchoolResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder()
                .add("schoolId", schoolId)
                .add("classYear", classYear)
                .add("token", token)
                .build();

        mRestApi.getClass(Constant.HOST + Constant.BASE_URL + Constant.METHOD_GET_CLASS, requestBody)
                .map(new Function<SchoolResponse, SchoolResponse>() {
                    @Override
                    public SchoolResponse apply(SchoolResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void isQualified(String token, Observer<BaseResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .build();

        mRestApi.isQualified(Constant.HOST + Constant.BASE_URL + Constant.MEHTOD_IS_QUALITFIED, requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getTaskWorkers(String token, String taskId, Observer<TaskWorkerResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .add("taskId", taskId)
                .build();

        mRestApi.getTaskWorker(Constant.HOST + Constant.BASE_URL + Constant.METHOD_GET_TASK_WORKER, requestBody)
                .map(new Function<TaskWorkerResponse, TaskWorkerResponse>() {
                    @Override
                    public TaskWorkerResponse apply(TaskWorkerResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void accept(String inviterId, String taskId, Observer<BaseResponse> subscriber) {


        Map<String, Object> map = new HashMap<>();//body  map
        map.put("inviterId", inviterId);
        map.put("taskId", taskId);
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);

        mRestApi.accept(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_ACCEPT, requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getUserInfo(Observer<UserInfoResponse> subscriber) {


        mRestApi.getUserInfo(Constant.USER_HOST + Constant.METHOD_GET_USER_INFO)
                .map(new Function<UserInfoResponse, UserInfoResponse>() {
                    @Override
                    public UserInfoResponse apply(UserInfoResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void updateUserInfo(String realName, String phone, String sex, String email, Observer<BaseResponse> subscriber) {


        Map<String, Object> map = new HashMap<>();//body  map
        map.put("realName", realName);
        map.put("phone", phone);
        map.put("sex", sex);
        map.put("email", email);
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);


        mRestApi.updateUserInfo(Constant.USER_HOST + Constant.METHOD_UPDATE_USER_INFO, requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void updatePassword(String oldPassword, String newPassword, Observer<BaseResponse> subscriber) {
        Map<String, Object> map = new HashMap<>();//body  map
        map.put("oldPassword", oldPassword);
        map.put("newPassword", newPassword);
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);

        mRestApi.updatePassword(Constant.USER_HOST + Constant.METHOD_UPDATE_PASSWORD, requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void isTask(String taskId, Observer<IsTaskDataResponse> subscriber) {


        mRestApi.isTask(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_IS_TASK, taskId)
                .map(new Function<IsTaskDataResponse, IsTaskDataResponse>() {
                    @Override
                    public IsTaskDataResponse apply(IsTaskDataResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void isPerm(String taskId, String token, Observer<IsTaskDataResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .add("taskId", taskId)
                .build();

        mRestApi.isPerm(Constant.HOST + Constant.BASE_URL + Constant.METHOD_IS_PERM, requestBody)
                .map(new Function<IsTaskDataResponse, IsTaskDataResponse>() {
                    @Override
                    public IsTaskDataResponse apply(IsTaskDataResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getQRCode(String taskId, Observer<QRCodeResponse> subscriber) {


        mRestApi.getQRCode(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_QR_CODE, taskId)
                .map(new Function<QRCodeResponse, QRCodeResponse>() {
                    @Override
                    public QRCodeResponse apply(QRCodeResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getDevices(long deviceType, Observer<DeviceResponse> subscriber) {


        mRestApi.getDevices(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_DEVICES, deviceType)
                .map(new Function<DeviceResponse, DeviceResponse>() {
                    @Override
                    public DeviceResponse apply(DeviceResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getDevicesType(Observer<DeviceResponse> subscriber) {


        mRestApi.getDevicesType(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_DEVICES_TYPE)
                .map(new Function<DeviceResponse, DeviceResponse>() {
                    @Override
                    public DeviceResponse apply(DeviceResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void compareFace(String fileBase64, String faceset_token, Observer<CompareFaceResponse> subscriber) {
//        MultipartBody.Builder multipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
//            multipartBody.addFormDataPart("image_file", fileBase64.getName(), RequestBody.create(MediaType.parse("image/jpg"), fileBase64));
//        multipartBody.addFormDataPart("api_key","nQYiXafeRtm9urBbtZz7NbG7B2J0EnSf");
//        multipartBody.addFormDataPart("api_secret","Dzmj8OvrnNsus7LDFJKHrBh6T7_GKLC-");
//        multipartBody.addFormDataPart("faceset_token","aa166ea4ae135e0c1332773e3b64864f");

        RequestBody requestBody = new FormBody.Builder()
                .add("api_key", "nQYiXafeRtm9urBbtZz7NbG7B2J0EnSf")
                .add("api_secret", "Dzmj8OvrnNsus7LDFJKHrBh6T7_GKLC-")
                .add("faceset_token", "aa166ea4ae135e0c1332773e3b64864f")
                .add("image_base64", fileBase64)
                .build();


        mRestApi.compareFace(Constant.METHOD_FACE_ID, requestBody)
                .map(new Function<CompareFaceResponse, CompareFaceResponse>() {
                    @Override
                    public CompareFaceResponse apply(CompareFaceResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
//                        if (!response.isSuccessful()) {
//                            throw new BusinessException(response.code, response.message);
//                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void updateFace(String studentId, File file, Observer<FaceResponse> subscriber) {
        MultipartBody.Builder multipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
        multipartBody.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file));
        multipartBody.addFormDataPart("studentId", studentId);


        mRestApi.updateFace(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_UPDATE_FACE, multipartBody.build())
                .map(new Function<FaceResponse, FaceResponse>() {
                    @Override
                    public FaceResponse apply(FaceResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void resetFace(String studentId, File file, Observer<FaceResponse> subscriber) {
        MultipartBody.Builder multipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
        multipartBody.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file));
        multipartBody.addFormDataPart("studentId", studentId);


        mRestApi.resetFace(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_RESET_FACE, multipartBody.build())
                .map(new Function<FaceResponse, FaceResponse>() {
                    @Override
                    public FaceResponse apply(FaceResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void detectFace(String taskId, File file, Observer<FaceResponse> subscriber) {
        MultipartBody.Builder multipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
        multipartBody.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file));
        multipartBody.addFormDataPart("taskId", taskId);


        mRestApi.detectFace(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_DETECT_FACE, multipartBody.build())
                .map(new Function<FaceResponse, FaceResponse>() {
                    @Override
                    public FaceResponse apply(FaceResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void createReview(String taskId, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder()
                .add("taskId", taskId)
                .build();


        mRestApi.createReview(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_CREATE_REVIEW, requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
//                        if (!response.isSuccessful()) {
//                            throw new BusinessException(response.code, response.message);
//                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void updateTask(String taskId, Observer<BaseResponse> subscriber) {

        Map<String, Object> map = new HashMap<>();//body  map
        map.put("taskId", taskId);
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);

        mRestApi.updateTask(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_UPDATE_TASK, requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
//                        if (!response.isSuccessful()) {
//                            throw new BusinessException(response.code, response.message);
//                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getCollectClass(String isFinish, String studentName, String taskId, Observer<CollectClassResponse> subscriber) {


        mRestApi.getCollectClassList(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_COLLECT_CLASS_LIST, isFinish, studentName, taskId, 1, 1000)
                .map(new Function<CollectClassResponse, CollectClassResponse>() {
                    @Override
                    public CollectClassResponse apply(CollectClassResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getCollectStudentList(String isFinish, String studentName, String taskId, String classId, Observer<CollectStudentResponse> subscriber) {


        mRestApi.getCollectStudentList(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_COLLECT_STUDENT_LIST, isFinish, classId, studentName, Long.parseLong(taskId), 1, 1000)
                .map(new Function<CollectStudentResponse, CollectStudentResponse>() {
                    @Override
                    public CollectStudentResponse apply(CollectStudentResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getStudentInfo(String taskId, String studentId, Observer<StudentInfoResponse> subscriber) {


        mRestApi.getStudentInfo(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_STUDENT_INFO, taskId, studentId)
                .map(new Function<StudentInfoResponse, StudentInfoResponse>() {
                    @Override
                    public StudentInfoResponse apply(StudentInfoResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void canOperateByPlanStatus(String orgId, Observer<BaseBooleanResponse> subscriber) {


        mRestApi.canOperateByPlanStatus(Constant.USER_HOST + Constant.METHOD_CANOPERATEBYPLANSTATUS, orgId)
                .map(new Function<BaseBooleanResponse, BaseBooleanResponse>() {
                    @Override
                    public BaseBooleanResponse apply(BaseBooleanResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void validStudent(String prefx, String orgId, String name, String idcard, String sex, String birth, String stuType, String isAdd, String classId, String classYear, Observer<ValidResponse> subscriber) {


        mRestApi.validStudent(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_ADD_VALID_STUDENT, prefx, orgId, name, idcard, sex, birth, stuType, isAdd, classId, classYear)
                .map(new Function<ValidResponse, ValidResponse>() {
                    @Override
                    public ValidResponse apply(ValidResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getDomainName(String account, Observer<IaBaseResponse> subscriber) {

        mRestApi.getDomainName(Constant.DOMAIN_HOST + Constant.METHOD_GET_DOMAIN_NAME, account)
                .map(new Function<IaBaseResponse, IaBaseResponse>() {
                    @Override
                    public IaBaseResponse apply(IaBaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getPlanYearList(Observer<YearResponse> subscriber) {

        mRestApi.getPlanYearList(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_PLANYEAR_LIST)
                .map(new Function<YearResponse, YearResponse>() {
                    @Override
                    public YearResponse apply(YearResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getStudentSEQ(String studentId, String taskId, Observer<StudentSEQ> subscriber) {
        Map<String, Object> map = new HashMap<>();
        map.put("studentId", studentId);
        map.put("taskId", taskId);
        String json = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(JSON, json);
        mRestApi.getStudentSEQ(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GET_STUDENT_SEQ, requestBody)
                .map(new Function<StudentSEQ, StudentSEQ>() {
                    @Override
                    public StudentSEQ apply(StudentSEQ response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getEyeVisionByBiology(String studentId, String taskId, Observer<AxisResponse> subscriber) {
        Map<String, Object> map = new HashMap<>();
        map.put("studentId", studentId);
        map.put("taskId", taskId);

        mRestApi.getEyeVisionByBiology(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_GETEYEVISIONBYBIOLOGY, JsonUtil.listToJson( map ) )
                .map(new Function<AxisResponse, AxisResponse>() {
                    @Override
                    public AxisResponse apply(AxisResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void addEyeVisionDataByBiology(Map<String, Object> map, Observer<QualifiedResponse> subscriber) {

        mRestApi.addEyeVisionDataByBiology(Constant.USER_HOST + Constant.BASE_URL + Constant.METHOD_ADDEYEVISIONDATABYBIOLOGY, JsonUtil.listToJson( map ))
                .map(new Function<QualifiedResponse, QualifiedResponse>() {
                    @Override
                    public QualifiedResponse apply(QualifiedResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}
